<?php
/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Framework
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Section_Exception
 */
importModule('Section.Exception');

/**
 * Class Hoathis_Section
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Section
 */
class Hoathis_Section {

    /*
       * Database object
       * @var $db Object
    */
    private $db = null;

    /*
       * Database table name
       * @var $db_table String
    */
    private $db_table = 'sections';

    /**
     * Constructor
     *
     * @param Int $idsection Section id (optionnal)
     * @return Boolean
     */
    public function __construct( $idsection = null ) {

        $this->db = Hoa_Database_Dal::getInstance('default');

        return $this->getById($idsection);
    }

    /**
     * Get section by id
     *
     * @param Int $idsection Id section
     * @return Boolean
     */
    public function getById ( $idsection = null ) {

        if (is_null($idsection) OR !is_int($title))
            return false;

        $statement = null;
        try {
            $statement = $this->db
            ->prepare('SELECT * FROM '.$this->db_table.' WHERE idsection = :idsection LIMIT 1')
            ->execute(array(':idsection' => $idsection));
            ->fetchAll();
        }
        catch ( Hoa_Database_Dal_Exception $e ) {

            $statement->closeCursor();
            return false;
        }

        if ($statement == 0)
            return false;

        foreach ($statement as $key => $val)
            $this->$key = $val;

        return true;
    }

    /**
     * Get section by title
     *
     * @param String $title Title section
     * @return Boolean
     */
    public function getByTitle ( $title = null ) {

        if (is_null($idsection) OR !is_string($title))
            return false;

        $statement = null;
        try {
            $statement = $this->db
            ->prepare('SELECT * FROM '.$this->db_table.' WHERE titre = :title LIMIT 1')
            ->execute(array(':title' => $title));
            ->fetchAll();
        }
        catch ( Hoa_Database_Dal_Exception $e ) {

            $statement->closeCursor();
            return false;
        }

        if ($statement == 0)
            return false;

        foreach ($statement as $key => $val)
            $this->$key = $val;

        return true;
    }
}